package com.ruoyi.buss.task.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.system.api.domain.SysFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.ruoyi.buss.system.domain.SysBusparams;
import com.ruoyi.buss.system.mapper.SysBusparamsMapper;
import com.ruoyi.buss.task.domain.DataTaskExRecord;
import com.ruoyi.buss.task.mapper.DataTaskExRecordMapper;
import com.ruoyi.buss.task.service.IDataTaskExRecordService;
import com.ruoyi.buss.utils.Params;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.system.api.model.LoginUser;

/**
 * 营销任务执行记录Service业务层处理
 * 
 * @author sxy
 * @date 2021-07-16
 */
@Service
public class DataTaskExRecordServiceImpl implements IDataTaskExRecordService 
{
    @Autowired
    private DataTaskExRecordMapper dataTaskExRecordMapper;
    @Autowired
    private SysBusparamsMapper sysBusparamsMapper;
    public List<DataTaskExRecord> listByTask(String taskId, String idCard,LoginUser userInfo) {
        String orgName = userInfo.getSysUser().getDept().getDeptName();
        DataTaskExRecord param = new DataTaskExRecord();
        param.setIdCard(idCard);
        if(StringUtils.isNotEmpty(taskId))
        {
            param.setTaskId(taskId);
        }
        /**
		 * 判断当前配置权限是否查询所有客户
		 */
        List<DataTaskExRecord> doList = dataTaskExRecordMapper.listByTask(param);
        SysBusparams sysBusparamApp = sysBusparamsMapper.selectSysBusparamsById(Params.IF_QUERY_APP_IMAGE_NEW);
        SysBusparams sysBusparamPc = sysBusparamsMapper.selectSysBusparamsById(Params.IF_QUERY_PC_IMAGE);
		String ifCheckApp = sysBusparamApp.getValue();
		String ifCheckPc = sysBusparamPc.getValue();
        System.err.println("ifCheckApp"+ifCheckApp);
        System.err.println("ifCheckPc"+ifCheckPc);
        for (DataTaskExRecord recordVO : doList) {


            List<SysFile> appFile = new ArrayList<SysFile>();
//            appFile = recordVO.getPcFileList();
            for(SysFile file : recordVO.getPcFileList()){
                SysFile tmp = new SysFile();
                String str = file.getUrl();
                if(str.indexOf("/test/files") != -1){
                    str = str.replace("32.14.33.6:5004","58.213.192.164:5009");
                }else if(str.indexOf("/jzyx/download") != -1){
                    str = "http://"+ifCheckApp+str.substring(str.indexOf("filePath=")+"filePath=".length());
                }else if(str.indexOf("/statics") != -1){
                    str = "http://"+ifCheckApp+str.substring(str.indexOf("statics")+"statics".length());
                }
                str = str.replace("fileName=/test/files","fileName=/phone/test/files");
                tmp.setName(recordVO.getId());
                tmp.setUrl(str);
                System.err.println(str);
                appFile.add(tmp);
            }

            List<SysFile> pcFile = new ArrayList<SysFile>();
//            pcFile =  recordVO.getPcFileList();
            for(SysFile file : recordVO.getPcFileList()){
                SysFile pcTmp = new SysFile();
                String pcStr = file.getUrl();
                if(pcStr.indexOf("/test/files") != -1){
                    pcStr = pcStr.replace("58.213.192.164:5009","32.14.33.6:5004");
                }else if(pcStr.indexOf("/jzyx/download") != -1){
                    pcStr = "http://"+ifCheckPc+pcStr.substring(pcStr.indexOf("filePath=")+"filePath=".length());
                }else if(pcStr.indexOf("/statics") != -1){
                    pcStr = "http://"+ifCheckPc+pcStr.substring(pcStr.indexOf("statics")+"statics".length());
                }
                pcStr = pcStr.replace("fileName=/test/files","fileName=/phone/test/files");
                pcTmp.setName(recordVO.getId());
                pcTmp.setUrl(pcStr);

                pcFile.add(pcTmp);
            }

            recordVO.setAppFileList(appFile);
            recordVO.setPcFileList(pcFile);
            recordVO.setExId(recordVO.getId());
            recordVO.setTaskName(recordVO.getTaskName());
        }
        return doList;
    }

//    public static void main(String[] args) {
//        String str = "http://58.213.192.164:5009/jzyx/download/file?filePath=/2021/08/09/692ec28ecb3dc80d37c9b2bc1fba2c9b/609455599796617216.jpg";
//        String str1 = "http://58.213.192.164:5009/buss/file/view?fileName=/test/files/2021/09/05/bd30ba82-e156-4323-92a5-e1e695d2351c.jpg";
//        String str2 = "http://32.172.33.11:9300/statics/2021/09/03/aec33218-178f-4be8-9c60-ef8aa5f92ac1.jpg";
//        test(str);
//        test(str1);
//        test(str2);
//
//    }
//    public static void test(String str){
//        String  ifCheckPc = "58.213.192.164:5009/buss/file/view?fileName=/test/files";
//        if(str.indexOf("/test/files") != -1){
//
//        }else if(str.indexOf("/jzyx/download") != -1){
//            str = "http://"+ifCheckPc+str.substring(str.indexOf("filePath=")+"filePath=".length());
//        }else if(str.indexOf("/statics") != -1){
//            str = "http://"+ifCheckPc+str.substring(str.indexOf("statics")+"statics".length());
//        }
//        System.out.println(str);
//    }
    /**
     * 查询营销任务执行记录
     * 
     * @param id 营销任务执行记录ID
     * @return 营销任务执行记录
     */
    @Override
    public DataTaskExRecord selectDataTaskExRecordById(String id)
    {
    	DataTaskExRecord dataTaskExRecord = new DataTaskExRecord();
    	dataTaskExRecord.setId(id);
        return dataTaskExRecordMapper.selectDataTaskExRecordById(dataTaskExRecord);
    }

    /**
     * 查询营销任务执行记录列表
     * 
     * @param dataTaskExRecord 营销任务执行记录
     * @return 营销任务执行记录
     */
    @Override
    public List<DataTaskExRecord> selectDataTaskExRecordList(DataTaskExRecord dataTaskExRecord)
    {
        return dataTaskExRecordMapper.selectDataTaskExRecordList(dataTaskExRecord);
    }

    /**
     * 新增营销任务执行记录
     * 
     * @param dataTaskExRecord 营销任务执行记录
     * @return 结果
     */
    @Override
    public int insertDataTaskExRecord(DataTaskExRecord dataTaskExRecord)
    {
        dataTaskExRecord.setCreateTime(DateUtils.getNowDate());
        return dataTaskExRecordMapper.insertDataTaskExRecord(dataTaskExRecord);
    }

    /**
     * 修改营销任务执行记录
     * 
     * @param dataTaskExRecord 营销任务执行记录
     * @return 结果
     */
    @Override
    public int updateDataTaskExRecord(DataTaskExRecord dataTaskExRecord)
    {
        dataTaskExRecord.setUpdateTime(DateUtils.getNowDate());
        return dataTaskExRecordMapper.updateDataTaskExRecord(dataTaskExRecord);
    }

    /**
     * 批量删除营销任务执行记录
     * 
     * @param ids 需要删除的营销任务执行记录ID
     * @return 结果
     */
    @Override
    public int deleteDataTaskExRecordByIds(String[] ids)
    {
        return dataTaskExRecordMapper.deleteDataTaskExRecordByIds(ids);
    }

    /**
     * 删除营销任务执行记录信息
     * 
     * @param id 营销任务执行记录ID
     * @return 结果
     */
    @Override
    public int deleteDataTaskExRecordById(String id)
    {
        return dataTaskExRecordMapper.deleteDataTaskExRecordById(id);
    }

    /**
     *  查询该营销任务，该客户是否有提交过营销反馈
     * @param taskExRecord
     * @param map
     * @return
     */
    public Map<String,Integer> countEX(DataTaskExRecord taskExRecord, Map<String,Integer>map) {
        if (StringUtils.isEmpty(taskExRecord.getTaskId())) {
        	DataTaskExRecord dataTaskExRecord = new DataTaskExRecord();
        	dataTaskExRecord.setId(taskExRecord.getId());
        	DataTaskExRecord item = dataTaskExRecordMapper.selectDataTaskExRecordById(dataTaskExRecord);
        	taskExRecord.setTaskId(item.getTaskId());
        	taskExRecord.setIdCard(item.getIdCard());
        }

        if (null == taskExRecord || StringUtils.isEmpty(taskExRecord.getTaskId()) || StringUtils.isEmpty(taskExRecord.getIdCard())) {
            return map;
        }

        int count = dataTaskExRecordMapper.countEX(taskExRecord);
        map.put("count",count);
        return map;
    }
}
